<template>
  <el-container class="home_container">
    <!-- 头 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>

      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左  侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle_button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="`/${subItem.path}`"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState(`/${subItem.path}`)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右 -->
      <el-main>
        <!-- 主题内容 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { getMenu } from "../utils/api";

export default {
  data() {
    // 左侧菜单数据
    return {
      menulist: [],
      iconsObj: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的连接地址
      activePath: "",
    };
  },
  methods: {
    // 退出 （原理是清空token）
    logout() {
      window.sessionStorage.clear("token");
      this.$router.push("/login");
      this.$message.success("退出成功！");
    },
    // 获取菜单
    async getMenuList() {
      const { data: res } = await getMenu();
      this.menulist = res.data;
      // console.log(res);
    },
    // 菜单展开隐藏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //  保存连接的激活状态
    saveNavState(activePath) {
      // 这里在后置导航守卫中存储  activePath会跟着path变化
      // window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
  // 在页面加载后立即获取菜单数据
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;

  .el-header {
    padding-left: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #373d41;
    > div {
      display: flex;
      align-items: center;
    }
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    span {
      margin-left: 15px;
      font-size: 20px;
      color: #fff;
    }
  }

  .el-aside {
    transition: all 0.3s;
    background: #333744;
    .iconfont {
      margin-right: 10px;
    }
    .el-menu {
      border-right: none;
    }
    .toggle_button {
      font-size: 10px;
      line-height: 24px;
      background: #4a5064;
      text-align: center;
      color: #fff;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }

  .el-main {
    background: #eaedf1;
  }
}
</style>